<?php
/**
 * upload.class.php     ZCMS 文件上传类
 * 
 * @copyright    (C) 2005 - 2010  ZCMS
 * @licenes      http://www.zcms.cc
 * @lastmodify   2010-11-15
 * @author       zhuayi  
 * @QQ			 ZCMS
 */

class upload
{

	//------允许上传附件大小
	var $upload_maxsize;

	/* ------附件存放路径 */
	var $upload_path;

	/* ------允许上传附件类型 */
	var $upload_allowext;
	
	/* 附件外链地址 */
	var $upload_url;
	
	/* ------上传文件 */
	function copy($file,$reset,$dir,$base)
	{
		
		$this->file = $file;
		
		/* ----检查状态 */
		$return = $this->condition();
		if ($return == '0')
		{
			return $reset;
		}
		
		/* ----检查类型 */
		$return = $this->type();
		if (!empty($return))
		{
			return $reset;
		}
		
		/* -----检查大小 */
		$return = $this->size();
		if (!empty($return))
		{
			return $reset;
		}
		
		/* -----获取文件名 */
		$this->base($base);
		
		/* ----转换路径 */
		$this->path = ZHUAYI_ROOT.$this->upload_path.$dir.'/';
		
		if (!file_exists($this->path))
		{
			mkdir($this->path,777,true);
		}
		/* ----连接路径 */
		$this->path .= strtolower($this->filename);
		
		/* -----复制文件 */
		if (!copy($this->file['tmp_name'],$this->path))
		{
			$this->error('拷贝文件出错');
		}

		return str_replace(ZHUAYI_ROOT,$this->upload_url,$this->path);
		
	}

	/* ------检查文件 */
	function size()
	{

		if ($this->file['size']> ($this->upload_maxsize*1024))
		{
			return $this->error('文件超出限制大小...');
		}
	}
	
	/* -----检查文件类型 */
	function type()
	{
		/* --------取的上传文件的后缀 */
		$h = strtolower(trim(substr(strrchr($this->file['name'],'.'),1,100)));
		
		/* --------转换定义的附件类型为数组，开始查找 */
		$this->upload_allowext = explode('|',$this->upload_allowext);
		
		if (!in_array($h,$this->upload_allowext))
		{
			return $this->error('不是允许上传的文件类型1');
		}
	}
	/* -----错误信息 */
	function error($error)
	{
		return  array('code_en'=>'-1','code_cn'=>$error);
		
	}
	
	/* -----获取文件名 */
	function base($former = '')
	{
		if ($former=='')
		$this->filename =  basename($this->file["name"]);
		else
		$this->filename = $former.'.'.trim(substr(strrchr($this->file['name'],'.'),1,100)); /* --------取的上传文件的后 */
	}
	
	/*检查状态 */
	function condition()
	{
		if ($this->file['error'] == 4)
		{
			return '0';
		}
	}
	

	

}
?>